package com.seven.mybatis.commons;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;


/**
 * 推荐只在前端调用Controller的接口层使用, 即对外提供服务的最外层.<br>
 *
 * @param <V>
 */
public class HttpResult<V> implements Serializable {


    private static final long serialVersionUID = -3708592576848534919L;

    /**
     * 接口返回状态
     * 0|成功,1|前台参数错误, 2|后台程序异常, 3|获取数据为空或者非预期数据,4|token失效, 5|其他
     */
    private Integer status;

    /**
     * 接口返回数据
     */
    private V result = null;

    /**
     * 异常时，返回的错误描述字符串
     */
    private String msg = null;

    /**
     * 构建httpResult
     *
     * @param status  返回结果状态
     * @param result  返回结果
     * @param message 返回提示信息
     * @return
     */
    private static HttpResult<Object> createHttpResult(Integer status, Object result, String message) {
    	HttpResult<Object> builder = new HttpResult<Object>();
        builder.setMsg(message);
        builder.setStatus(status);
        if (result == null) {
            result = new HashMap<>();
        }
        builder.setResult(result);
        return builder;
    }

    /**
     * 创建token失效结果
     *
     * @return
     */
    public static HttpResult<Object> createInvalidTokenResult() {
        return createHttpResult(AppStatusEnum.INVALID_TOKEN.getCode(), null, AppStatusEnum.INVALID_TOKEN.getValue());
    }

    /**
     * 创建会话失效结果
     *
     * @return
     */
    public static HttpResult<Object> createSessionInvalidResult() {
        return createHttpResult(AppStatusEnum.SESSION_INVALID.getCode(), null, AppStatusEnum.SESSION_INVALID.getValue());
    }

    /**
     * 创建会话失效结果
     *
     * @return
     */
    public static HttpResult<Object> createSessionInvalidResult(String message) {
        return createHttpResult(AppStatusEnum.SESSION_INVALID.getCode(), null, message);
    }

    /**
     * 创建请求参数错误结果
     *
     * @return
     */
    public static HttpResult<Object> createParamErrorResult() {
        return createParamErrorResult((Object) null);
    }

    /**
     * 创建请求参数错误结果
     *
     * @return
     */
    public static HttpResult<Object> createParamErrorResult(String message) {
        return createHttpResult(AppStatusEnum.PARAM_ERROR.getCode(), null, message);
    }

    /**
     * 创建请求参数错误结果
     *
     * @return
     */
    public static HttpResult<Object> createParamErrorResult(Object result) {
        return createHttpResult(AppStatusEnum.PARAM_ERROR.getCode(), result, AppStatusEnum.PARAM_ERROR.getValue());
    }
    


    /**
     * 返回文本提示结果
     *
     * @return
     */
    public static HttpResult<Object> createMessageResult() {
        return createMessageResult((Object) null);
    }

    /**
     * 返回文本提示结果
     *
     * @param message
     * @return
     */
    public static HttpResult<Object> createMessageResult(String message) {
        return createMessageResult(null, message);
    }

    /**
     * 返回文本提示结果
     *
     * @param result
     * @return
     */
    public static HttpResult<Object> createMessageResult(Object result) {
        return createMessageResult(result, AppStatusEnum.NOT_EXPECT_DATA.getValue());
    }

    /**
     * 返回文本提示和结果对象
     *
     * @param message
     * @return
     */
    public static HttpResult<Object> createMessageResult(Object result, String message) {
        return createHttpResult(AppStatusEnum.NOT_EXPECT_DATA.getCode(), result, message);
    }

    /**
     * 业务处理成功后，通过这个方法包装返回对象
     *
     * @return
     */
    public static HttpResult<Object> createSuccessResult() {
        return createSuccessResult((Object) null);
    }

    /**
     * 业务处理成功后，通过这个方法包装返回对象
     *
     * @param message
     * @return
     */
    public static HttpResult<Object> createSuccessResult(String message) {
        return createSuccessResult(null, message);
    }

    /**
     * 业务处理成功后，通过这个方法包装返回对象
     *
     * @param result
     * @return
     */
    public static HttpResult<Object> createSuccessResult(Object result) {
        return createSuccessResult(result, AppStatusEnum.SUCCESS.getValue());
    }

    /**
     * 业务处理成功后，通过这个方法包装返回对象
     *
     * @param result
     * @param message
     * @return
     */
    public static HttpResult<Object> createSuccessResult(Object result, String message) {
        return createHttpResult(AppStatusEnum.SUCCESS.getCode(), result, message);
    }

    /**
     * 当前网络不稳定
     *
     * @return
     */
    public static HttpResult<Object> createExceptionErrorResult() {
        return createExceptionErrorResult((Object) null);
    }

    /**
     * 当前网络不稳定
     *
     * @return
     */
    public static HttpResult<Object> createExceptionErrorResult(String message) {
        return createHttpResult(AppStatusEnum.EXCEPTION_ERROR.getCode(), null, message);
    }

    /**
     * 当前网络不稳定
     *
     * @return
     */
    public static HttpResult<Object> createExceptionErrorResult(Object result) {
        return createHttpResult(AppStatusEnum.EXCEPTION_ERROR.getCode(), result, AppStatusEnum.EXCEPTION_ERROR.getValue());
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public V getResult() {
        return result;
    }

    public void setResult(V result) {
        this.result = result;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}
